package hzau.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deepoove.poi.XWPFTemplate;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import hzau.dao.PaperDao;
import hzau.dao.QuestionDao;
import hzau.dao.SelectionQuestionDao;
import hzau.pojo.Paper;
import hzau.pojo.question.Question;
import hzau.pojo.question.SelectionQuestion;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//@SpringBootTest
@Configuration
public class WordGenUtil {

    @Autowired
    SelectionQuestionDao selectionQuestionDao;
    @Autowired
    PaperDao paperDao;
    @Autowired
    QuestionDao questionDaoDao;
//    @Test
    public  String genWord(
            Long paperId
    ) throws Exception {
        int num=1;
        Paper paper = paperDao.selectById(paperId);

        HashMap<String, Object> map=new HashMap<>();
        map.put("name", paper.getName());
        List<SelectionQuestion> selections = getSelections(paper.getId());
        if(selections.size()!=0){
            map.put("selectionsSeq",getQuestionPos(num));
            num++;
            map.put("selectName","、选择题");
            map.put("selectQuestions", selections);}
        for(int i=1;i<=4;i++){
            List<Question> question = getQuestion(paper.getId(), (short) i);
            if(question.size()==0)continue;
            else {
                if(i==1){
                    map.put("judgeQuestionSeq",getQuestionPos(num));
                    num++;
                    map.put("judgeQuestionName","、判断题");
                    map.put("judgement", question);
                }
                if(i==2){
                    map.put("gapFillingSeq",getQuestionPos(num));
                    num++;
                    map.put("gapFillingName","、填空题");
                    map.put("gapFilling", question);
                }
                if(i==3){
                    map.put("essaySeq",getQuestionPos(num));
                    num++;
                    map.put("essayName","、论述题");
                    map.put("essay", question);
                }
                if(i==4){
                    map.put("shortAnswerSeq",getQuestionPos(num));
                    num++;
                    map.put("shortAnswerName","、简答题");
                    map.put("shortAnswer", question);
                }
            }
        }
        XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\user\\Desktop\\毕设\\university_test_paper_system\\University\\src\\main\\resources\\templates\\paper.docx").render(
                map   );
        template.writeAndClose(new FileOutputStream("output.docx"));
        MultipartFile file = fileToMultipartFile(new File("output.docx"));
        String url = OssUtils.upload(file);
        paper.setUrl(url);
        paperDao.updateById(paper);
        return url;

    }
    public List<SelectionQuestion> getSelections(Long paperId){
        LambdaQueryWrapper<SelectionQuestion> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SelectionQuestion::getPaperId,paperId);
        List<SelectionQuestion> selectionQuestions = selectionQuestionDao.selectList(lqw);
        return selectionQuestions;
    }
    public List<Question> getQuestion(Long paperId,Short type){
        LambdaQueryWrapper<Question> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Question::getPaperId,paperId);
        lqw.eq(Question::getType,type);
        List<Question> selectionQuestions = questionDaoDao.selectList(lqw);
        return selectionQuestions;
    }
    public String getQuestionPos(int pos){
        switch (pos){
            case 1: return "一";

            case 2: return "二";

            case 3: return "三";

            case 4: return "四";

            case 5: return "五";

//            case 1: return "一";
        }
        return "零";
    }
    public MultipartFile fileToMultipartFile(File file) {
        FileItem fileItem = createFileItem(file);
        MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
        return multipartFile;
    }

    private static FileItem createFileItem(File file) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item = factory.createItem("textField", "text/plain", true, file.getName());
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try {
            FileInputStream fis = new FileInputStream(file);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return item;
    }


}
